﻿using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Simple.Common.Filters;
using Simple.Common.Middlewares;
using Simple.Services.Account.Models;
using ZXing.Common;

namespace Simple.WebApi.Controllers;

/// <summary>
/// 账号管理
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[DisabledRequestRecord]
public class AccountController : ControllerBase
{
    private readonly AccountService _accountService;
    private readonly ILogger _logger;


    public AccountController(AccountService accountService, ILogger<ApiExceptionMiddleware> logger)
    {
        _accountService = accountService;
        _logger = logger;
    }

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="login"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Login([FromBody] LoginModel login)
    {
        string token = await _accountService.GetTokenAsync(login);

        return AppResult.Status200OK("成功", token);
    }

    /// <summary>
    /// 获取用户信息
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Authorize]
    public async Task<AppResult> GetUserInfo()
    {
        var data = await _accountService.GetUserInfoAsync();
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 登出
    /// </summary>
    /// <param name="account"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> LogOut(string account)
    {
        await _accountService.LogOut(account);
        return AppResult.Status200OK("登出成功");
    }

    /// <summary>
    /// APP登录
    /// </summary>
    /// <param name="login"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> AppLogin([FromBody] AppLoginModel login)
    {
        var data = await _accountService.AppLogin(login);

        return AppResult.Status200OK("成功", data);
    }

    /// <summary>
    /// 获取微信openid
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> GetWechatInfo([FromQuery] string code)
    {
        var data = await _accountService.GetWeChatInfo(code);

        return AppResult.Status200OK("成功", data);
    }

    /// <summary>
    /// 微信文本检测
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> WechatMsgCheck([FromBody] WechatMsgSecCheckInput input)
    {
        var data = await _accountService.WechatMsgSecCheck(input);

        return AppResult.Status200OK("成功", data);
    }

    /// <summary>
    /// 微信音影检测
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> WechatMediaCheck([FromBody] WechatMediaCheckInput input)
    {
        var data = await _accountService.WechatMediaCheck(input);

        return AppResult.Status200OK("成功", data);
    }

    /// <summary>
    /// 获取微信openid
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> GetWechatToken()
    {
        var data = await _accountService.WechatGetToken();

        return AppResult.Status200OK("成功", data);
    }
     
}
